<?php
namespace Admin\Controller;

use Think\Controller;

class CommonController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->check_login();
    }

    public function check_login()
    {
        if ($user = session('user')) {
            $this->assign('user', $user);
            return;
        }

        if (!$token = cookie('token')) {
            $this->error('你还没有登录', U("Admin/User/login"));
        }

        $User = M("User");
        $user = $User->where("token='$token'")->find();
        if (!$user) {
            $this->error('非法登录', U("Admin/User/login"));
        }

        session('user', $user);
        $this->assign('user', $user);
    }
}